package nba2kmodtool.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class ProgressDialog extends JDialog {

	private static final long serialVersionUID = 1116424058223480751L;
	private JProgressBar progressBar;
	private JLabel label;

	public static void main(String[] args) {
		try {
			JFrame mainFrame = new JFrame();
			mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			mainFrame.setSize(new Dimension(640, 480));
			ProgressDialog frame = new ProgressDialog(null);
			//mainFrame.pack();
			mainFrame.setVisible(true);
			//frame.setModal(true);
			for (int i = 0; i < 11; i++) {
				frame.changeValue(i * 10);
				Thread.sleep(1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ProgressDialog(Container ownere) {
		super();
		initComponents();
		setTitle("Progress...");
	}

	public void changeValue(float value) {
		if (!isVisible()) {
			setVisible(true);
		}
		if (value >= 100) {
			dispose();
		}
		int progress = (int) (value * 10);
		if (Math.abs(progress - progressBar.getValue()) < 5) {
			return;
		}
		progressBar.setValue(progress);
		String desc = String.format("Completed：%10.1f", value) + '%';
		progressBar.setString(desc);
		label.setText(desc);
		update(getGraphics());
	}

	private void initComponents() {
		//set properties
		setSize(250, 100);
		setResizable(false);
		//setClosable(false);
		setTitle("Progress:");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//setUndecorated(true);		
		setAlwaysOnTop(true);
		//set components
		Container contentPane = getContentPane();
		this.label = new JLabel(" ", JLabel.CENTER);
		label.setForeground(Color.blue);
		this.progressBar = new JProgressBar(0, 1000);
		progressBar.setPreferredSize(new Dimension(200, 20));
		progressBar.setOrientation(JProgressBar.HORIZONTAL);
		progressBar.setStringPainted(true);
		progressBar.setBorderPainted(true);
		progressBar.setDoubleBuffered(true);
		contentPane.add(progressBar, BorderLayout.CENTER);
		contentPane.add(label, BorderLayout.SOUTH);
		pack();
	}
}
